/**
 * 功能:TK_PURCHASE_PAY 控制器类
 * 开发人员:chengd
 * 创建时间:2012-05-12 17:43:27
 */
package com.towker.frame.controller.purchase;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.towker.web.MyRequest;
import com.towker.core.page.PageModel;
import com.towker.core.page.PageNav;
import com.towker.core.util.CollectionUtil;
import com.towker.frame.controller.BaseController;
import com.towker.frame.model.purchase.PurchasePay;
import com.towker.frame.model.purchase.PurchasePlan;
import com.towker.frame.service.basicData.DepartmentsService;
import com.towker.frame.service.purchase.PurchasePayService;
import com.towker.frame.share.sys.user.UserShare;

//TK_PURCHASE_PAY
@Controller
@SuppressWarnings("unchecked")
@RequestMapping("/manage/purchase")
public class PurchasePayController extends BaseController
{
	private PurchasePayService service;
	private DepartmentsService departmentsService;//部门信息

	//业务对象注入
	public void setPurchasePayService(PurchasePayService service)
	{
		this.service = service;
	}
	
	public void setDepartmentsService(DepartmentsService departmentsService)
	{
		this.departmentsService = departmentsService;
	}

	//添加
	@RequestMapping("/addPurchasePay{viewName}")
	public ModelAndView addPurchasePay(HttpServletResponse response, HttpServletRequest request, PurchasePay po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			result = new ModelAndView();
			Map map = new HashMap();
			List departList = departmentsService.findByCoCode(UserShare.getCurrentUserCoCode(request));//部门信息
			result.addObject("departList", departList);
			result.addObject("pay_sn", service.getNewPaySn());//单号
			result.setViewName("/manage/purchase/addPurchasePay.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			
			po.setAuthCoCode(UserShare.getCurrentUserCoCode(request));
			//po.setAuthDepCode(UserShare.getCurrentUserDepCode(request));
			
			try
			{
				service.add(po);
				writer.write("添加采购付款申请成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("添加采购付款申请失败！" + e.getCause());
			}
		}
		return result;
	}

	//删除
	@RequestMapping("/delPurchasePay")
	public void delPurchasePay(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		MyRequest req = new MyRequest(request);
		service.delByIds(CollectionUtil.toLongArray(req.getLongArray("keyIndex", 0)));
		response.sendRedirect(req.getRefererURL());
	}

	//修改
	@RequestMapping("/updPurchasePay{viewName}")
	public ModelAndView updPurchasePay(HttpServletRequest request, HttpServletResponse response, PurchasePay po, @PathVariable String viewName) throws IOException
	{
		ModelAndView result = null;
		if ("1".equals(viewName))
		{
			MyRequest req = new MyRequest(request);
			Long id = req.getLong("keyIndex");
			po = service.getById(id);
			result = new ModelAndView();
			Map map = new HashMap();
			List departList = departmentsService.findByCoCode(UserShare.getCurrentUserCoCode(request));//部门信息
			result.addObject("departList", departList);
			result.addObject("po", po);
			result.setViewName("/manage/purchase/updPurchasePay.jsp");
		}
		else if ("2".equals(viewName))
		{
			PrintWriter writer = response.getWriter();
			try
			{
				service.update(po);
				writer.write("修改采购付款申请成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("修改采购付款申请失败！" + e.getCause());
			}
		}
		return result;
	}

	//获得分页
	@RequestMapping("/getPurchasePay")
	public ModelAndView getPurchasePay(HttpServletRequest request,HttpServletResponse response, @RequestParam(value="page", defaultValue="1")int page)
	{
		int pageSize = 10;
		MyRequest req = new MyRequest(request);
		Map map = req.getParameterValueMap(false, true);
		map.put("authCoCode", UserShare.getCurrentUserCoCode(request));
		//map.put("authDepCode", UserShare.getCurrentUserDepCode(request));
		PageModel<PurchasePay> pageModel = service.getPage(page, pageSize, map);
		ModelAndView result = new ModelAndView();
		PageNav pageNav = new PageNav(request, pageModel);
		List departList = departmentsService.findByCoCode(UserShare.getCurrentUserCoCode(request));//部门信息
		result.addObject("departList", departList);
		result.addObject("pageModel", pageModel);
		result.addObject("pageNav", pageNav);
		result.setViewName("/manage/purchase/getPurchasePay.jsp");
		return result;
	}

	//明细
	@RequestMapping("/getPurchasePayById")
	public ModelAndView getPurchasePayById(HttpServletRequest request, HttpServletResponse response)
	{
		PurchasePay po = null;
		MyRequest req = new MyRequest(request);
		Long id = req.getLong("keyIndex");
		po = service.getById(id);
		ModelAndView result = new ModelAndView();
		result.addObject("po", po);
		result.setViewName("/manage/purchase/getPurchasePayById.jsp");
		return result;
	}
	
	//递交下一级审核
	@RequestMapping("/submitPay{viewName}")
	public ModelAndView submitPay(HttpServletRequest request, HttpServletResponse response ,PurchasePay po, @PathVariable String viewName) throws IOException{
		ModelAndView result = null;
		if ("1".equals(viewName)) {
			result = new ModelAndView();
			MyRequest req = new MyRequest(request);
			Long id = req.getLong("keyIndex");
			po = service.getById(id);
			result.addObject("po", po);
			Map map = new HashMap();
			List departList = departmentsService.findByCoCode(UserShare.getCurrentUserCoCode(request));//部门信息
			result.addObject("departList", departList);
			result.setViewName("/manage/purchase/submitPurchasePay.jsp");
		}else if ("2".equals(viewName)) {
			PrintWriter writer = response.getWriter();
			try
			{
				po.setStatus(1);
				service.update(po);
				writer.write("递交到下一级成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("递交到下一级失败！" + e.getCause());
			}
		}
		return result;
	}
	
	//审核
	@RequestMapping("/auditPay{viewName}")
	public ModelAndView auditPay(HttpServletRequest request, HttpServletResponse response ,PurchasePay po, @PathVariable String viewName) throws IOException{
		ModelAndView result = null;
		if ("1".equals(viewName)) {
			result = new ModelAndView();
			MyRequest req = new MyRequest(request);
			Long id = req.getLong("keyIndex");
			po = service.getById(id);
			result.addObject("po", po);
			Map map = new HashMap();
			List departList = departmentsService.findByCoCode(UserShare.getCurrentUserCoCode(request));//部门信息
			result.addObject("departList", departList);
			result.setViewName("/manage/purchase/auditPurchasePay.jsp");
		}else if ("2".equals(viewName)) {
			PrintWriter writer = response.getWriter();
			try
			{
				po.setStatus(2);
				po.setAudit_user(UserShare.getCurrentUserCode(request));//审核人员代码
				service.update(po);
				writer.write("审核成功！");
			}
			catch (Exception e)
			{
				e.printStackTrace();
				writer.write("审核失败！" + e.getCause());
			}
		}
		return result;
	}
	
	//采购完成
	@RequestMapping("/okPay")
	public void okPay(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		MyRequest req = new MyRequest(request);
		Long id = req.getLong("keyIndex");
		PurchasePay po = service.getById(id);
		po.setStatus(3);
		service.update(po);
		response.sendRedirect(req.getRefererURL());
	}
	
}
